package ywpt.web.action.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.DateTools;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.RepairInfo;
import ywpt.model.SysMsgInfo;
import ywpt.service.service.RepairInfoService;
import ywpt.service.work.SysMsgInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class RepairInfoAction  extends BaseAction implements ModelDriven<RepairInfo>, Preparable{

	/**
	 * 设备维修信息
	 */
	private static final long serialVersionUID = 8194338635512226127L;
	private static final int DEFAULT_PAGE_SIZE = 100;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	private Long id;	
	
	private RepairInfo repairInfo;
	private List<RepairInfo> repairInfoList = new ArrayList<RepairInfo>();
	private RepairInfoService repairInfoService;
	
	private int version;
	
	private List<SysMsgInfo> sysMsgInfoList = new ArrayList<SysMsgInfo>();
	private SysMsgInfoService sysMsgInfoService;
	
	private Long jobId;
	private String jobName;
	private Long clientId;
	private String clientName;
	private Long deviceId;
	private String deviceName;
	private Long getUserId;
	private String getUserName;
	private Long sendUserId;
	private String sendUserName;
	private Long responseId;
	private Long typeId;
	private Long resultId;
	

	//查询相关
	private String qClient;
	private String qDevice;
	private String qProblem;
	private String qStartDate;
	private String qEndDate;
	
	
	public String getQClient() {
		return qClient;
	}

	public void setQClient(String client) {
		qClient = client;
	}

	public String getQDevice() {
		return qDevice;
	}

	public void setQDevice(String device) {
		qDevice = device;
	}

	public String getQProblem() {
		return qProblem;
	}

	public void setQProblem(String problem) {
		qProblem = problem;
	}

	public String getQStartDate() {
		return qStartDate;
	}

	public void setQStartDate(String startDate) {
		qStartDate = startDate;
	}

	public String getQEndDate() {
		return qEndDate;
	}

	public void setQEndDate(String endDate) {
		qEndDate = endDate;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}
 
	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	public Long getSendUserId() {
		return sendUserId;
	}

	public void setSendUserId(Long sendUserId) {
		this.sendUserId = sendUserId;
	}

	public String getSendUserName() {
		return sendUserName;
	}

	public void setSendUserName(String sendUserName) {
		this.sendUserName = sendUserName;
	}

	public Long getResponseId() {
		return responseId;
	}

	public void setResponseId(Long responseId) {
		this.responseId = responseId;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public Long getResultId() {
		return resultId;
	}

	public void setResultId(Long resultId) {
		this.resultId = resultId;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public RepairInfo getRepairInfo() {
		return repairInfo;
	}

	public void setRepairInfo(RepairInfo repairInfo) {
		this.repairInfo = repairInfo;
	}

	public List<RepairInfo> getRepairInfoList() {
		return repairInfoList;
	}

	public void setRepairInfoList(List<RepairInfo> repairInfoList) {
		this.repairInfoList = repairInfoList;
	}

	public RepairInfoService getRepairInfoService() {
		return repairInfoService;
	}

	public void setRepairInfoService(RepairInfoService repairInfoService) {
		this.repairInfoService = repairInfoService;
	}	
	
	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}
	
	public SysMsgInfoService getSysMsgInfoService() {
		return sysMsgInfoService;
	}

	public void setSysMsgInfoService(SysMsgInfoService sysMsgInfoService) {
		this.sysMsgInfoService = sysMsgInfoService;
	}
	
	public List<SysMsgInfo> getSysMsgInfoList() {
		return sysMsgInfoList;
	}

	public void setSysMsgInfoList(List<SysMsgInfo> sysMsgInfoList) {
		this.sysMsgInfoList = sysMsgInfoList;
	}

	public Long getGetUserId() {
		return getUserId;
	}

	public void setGetUserId(Long getUserId) {
		this.getUserId = getUserId;
	}

	public String getGetUserName() {
		return getUserName;
	}

	public void setGetUserName(String getUserName) {
		this.getUserName = getUserName;
	} 
	/*------------------------------------------------------------------------------------*/
	
	@Override
	public RepairInfo getModel() {
		return this.repairInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.repairInfo = this.repairInfoService.getRepairInfoById(id);
		}else{
			this.repairInfo = new RepairInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.SERVICE_REPAIR);
		System.out.println("11111111111+++++++++");
		System.out.println("11111111111+++++++++"+popedom);
	}

	protected void getDataList(DetachedCriteria query) {
		if (popedom[3]) {
			//query.createAlias("creater", "creater").add(Restrictions.eq("creater.id", this.getCurrentUser().getPeople().getId()));
		}else {
			query.createAlias("creater", "creater");
			query.createAlias("responseUser", "responseUser");
			query.add(Restrictions.or(Restrictions.eq("responseUser.id", this.getCurrentUser().getPeople().getId()), 
					Restrictions.eq("creater.id", this.getCurrentUser().getPeople().getId())));
		}
		query.add(Restrictions.eq("deleteFlag", false));
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = repairInfoService.getCallRecordCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		repairInfoList.addAll(repairInfoService.getRepairInfos(query, pageSize, rowStartEnd[0]));
	}
	
	protected void showInfo() {
		if (repairInfo.getJob()!=null) {
			this.jobId = repairInfo.getJob().getId();
			this.jobName = repairInfo.getJob().getJobCode();
		}
		if (repairInfo.getClient()!=null) {
			this.clientId = repairInfo.getClient().getId();
			this.clientName = repairInfo.getClient().getClientName();
		}
		if (this.repairInfo.getDevice()!=null) {
			this.deviceId = this.repairInfo.getDevice().getId();
			this.deviceName = this.repairInfo.getDevice().getName();
		}
		if (this.repairInfo.getSendUser()!=null) {
			this.sendUserId = this.repairInfo.getSendUser().getId();
			this.sendUserName = this.repairInfo.getSendUser().getPeopleName();
		}
		if (this.repairInfo.getResponseUser()!=null) {
			this.responseId = this.repairInfo.getResponseUser().getId();
		}
		if (this.repairInfo.getRepareType()!=null) {
			this.typeId = this.repairInfo.getRepareType().getId();
		}
		if (this.repairInfo.getRepareResult()!=null) {
			this.resultId = this.repairInfo.getRepareResult().getId();
		}
		if(this.repairInfo.getGetUser()!=null){
			this.getUserId = this.repairInfo.getGetUser().getId();
			this.getUserName = this.repairInfo.getGetUser().getPeopleName();
		}
		this.sysMsgInfoList.addAll(this.sysMsgInfoService.getMsgs(3, this.repairInfo.getId()));
	}
 /*==============================================================================================*/
	
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
		getDataList(query);
		return SUCCESS;
	}
	
	public String input() {
		repairInfo.setRegisterDate(new Date());
		return INPUT;
	}
	
	public String edit() {
		this.repairInfo = this.repairInfoService.getRepairInfoById(id);
		showInfo();
		return INPUT;
	}

	public String save() throws Exception{
		try 
		{
			if (id == null) 
			{
				repairInfo.setCode(repairInfoService.ceratRepairCode());
				repairInfo.setCreater(this.getCurrentUser().getPeople());
				repairInfo.setCreateDate(new Date());
				repairInfoService.saveRepairInfo(repairInfo,jobId,clientId,deviceId,getUserId,sendUserId,responseId,typeId,resultId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} 
			else 
			{   
				if(!repairInfo.getCheckFlag())
				{
					if(this.getVersion()==repairInfo.getVersion())
					{
						repairInfo.setModifyer(this.getCurrentUser());
						repairInfo.setModifyDate(new Date());
						repairInfoService.updateRepairInfo(repairInfo,jobId,clientId,deviceId,getUserId,sendUserId,responseId,typeId,resultId);
						this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
					}
					else
						this.addActionMessage(SystemMessage.UPDATE_FAIL);
				}
				else
					this.addActionMessage(SystemMessage.AUDITING_SAVE);
			}
			showInfo();
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
				this.addActionError(e.getMessage());
			return INPUT;
		}
	}
	
	public String delete() {
		if(id!=null){
			this.repairInfo = this.repairInfoService.getRepairInfoById(id);
			showInfo();
			if(!repairInfo.getCheckFlag()){
				this.repairInfoService.removeRepairInfo(id);
				this.sysMsgInfoService.invalidMsg(3, id);
				return SUCCESS;
			}else{
				this.addActionError(SystemMessage.DELETE_AUDITING_FAIL);
				return INPUT;
			}
				
		}else{
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}
	
	public String submit() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
		} else {
			this.repairInfo = this.repairInfoService.getRepairInfoById(id);
			if (!repairInfo.getSubmitFlag()) {
				repairInfo.setSubmitFlag(true);
				this.repairInfoService.updateRepairInfo(repairInfo, null, null,null, null, null, null, null, null);
				if (repairInfo.getResponseUser()!=null) {
					sysMsgInfoService.invalidMsg(3, id);
					addSysMsg(repairInfo);					
				}
				this.addActionMessage(SystemMessage.SUMBIT_SUCCESS);
			} else {
				this.addActionMessage(SystemMessage.SUMBIT_REPEAT);
			}
		}
		showInfo();
		return SUCCESS;
	}
	
	public String disSubmit() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
		} else {
			this.repairInfo = this.repairInfoService.getRepairInfoById(id);
			if (!repairInfo.getCheckFlag()) {
				if (repairInfo.getSubmitFlag()) {
					repairInfo.setSubmitFlag(false);
					this.repairInfoService.updateRepairInfo(repairInfo, null, null,null, null, null, null, null, null);
					if (repairInfo.getResponseUser()!=null) {
						sysMsgInfoService.invalidMsg(3, id);
						addDisSubmitSysMsg(repairInfo);						
					}
					this.addActionMessage(SystemMessage.DISSUMBIT_SUCCESS);
				} else {
					this.addActionMessage(SystemMessage.DISSUMBIT__REPEAT);
				}
			} else {
				this.addActionMessage(SystemMessage.AUDITING_SAVE);
			}
			
		}
		showInfo();
		return SUCCESS;
	}
	
	
	public String check() {
		if(id!=null){
			this.repairInfo = this.repairInfoService.getRepairInfoById(id);
			showInfo();
			if(!repairInfo.getCheckFlag()){
				if (repairInfo.getSubmitFlag()) {
					this.repairInfoService.checkRepairInfo(id);
					this.sysMsgInfoService.invalidMsg(3, id);
					this.addActionMessage(SystemMessage.AUDITING_SUCCESS);					
				} else {
					this.addActionMessage("该记录尚未提交，不能审核！");	
				}
			}else
				this.addActionError(SystemMessage.AUDITING_REPEAT);
		}else
			this.addActionMessage(SystemMessage.NO_OBJECT);
		showInfo();
		return SUCCESS;
	}
	
	public String idsCheck() {
		if(id!=null){
			this.repairInfo = this.repairInfoService.getRepairInfoById(id);
			showInfo();
			if(repairInfo.getCheckFlag()){
				this.repairInfoService.disCheckRepairInfo(id);
				this.addActionMessage(SystemMessage.DISAUDITING_SUCCESS);
			}else
				this.addActionError(SystemMessage.DISAUDITING_REPEAT);
		}else
			this.addActionMessage(SystemMessage.NO_OBJECT);
		showInfo();
		return SUCCESS;
	}

	public String search() {
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
			if (!StringTools.isNullOrSpace(this.qClient)) {
				query.createAlias("client", "client").add(Restrictions.like("client.clientName", "%"+this.qClient+"%"));
			}
			if (!StringTools.isNullOrSpace(this.qDevice)) {
				query.createAlias("device", "device").add(Restrictions.like("device.name", "%"+this.qDevice+"%"));
			}
			if (!StringTools.isNullOrSpace(this.qProblem)) {
				query.add(Restrictions.like("faultDesc", "%"+this.qProblem+"%"));
			}
			if(!StringTools.isNullOrSpace(this.getQStartDate())&& !StringTools.isNullOrSpace(this.getQEndDate()))
			{				
				if(DateTools.strToDate(this.getQStartDate(), "yyyy-MM-dd").before(DateTools.strToDate(this.getQEndDate(), "yyyy-MM-dd")))
					query.add(Restrictions.between("registerDate",DateTools.strToDate(this.getQStartDate(), "yyyy-MM-dd"),DateTools.addDay(DateTools.strToDate(this.getQEndDate(), "yyyy-MM-dd"), 1)));
				else  
					query.add(Restrictions.between("registerDate",DateTools.strToDate(this.getQEndDate(), "yyyy-MM-dd"),DateTools.addDay(DateTools.strToDate(this.getQStartDate(), "yyyy-MM-dd"),1)));
			}
			else if((StringTools.isNullOrSpace(this.getQStartDate())&&!StringTools.isNullOrSpace(this.getQEndDate()))||
					!(StringTools.isNullOrSpace(this.getQStartDate())&&StringTools.isNullOrSpace(this.getQEndDate())))
			{
				this.addActionMessage("开始时间和结束时间都要填写！");
				return SUCCESS;
			}
			getDataList(query);
		return SUCCESS;
		
	}
	
	protected void addSysMsg(RepairInfo repairInfo) {
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("维修信息");
		sysMsgInfo.setMsgContent(repairInfo.getResponseUser().getPeopleName()+"提交了一条维修信息，正等待审核！");
		sysMsgInfo.setReceiveUser(repairInfo.getCreater().getId() +",");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setBusinessType(3);
		sysMsgInfo.setBusinessId(id);
		sysMsgInfo.setUrl("/device/repairInfo/edit-"+id+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(this.getCurrentUser());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}
	
	protected void addDisSubmitSysMsg(RepairInfo repairInfo){
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("维修信息");
		sysMsgInfo.setMsgContent(repairInfo.getCreater().getPeopleName()+"退回了一条维修信息，正等待处理！");
		sysMsgInfo.setReceiveUser(repairInfo.getResponseUser().getId() +",");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setBusinessType(3);
		sysMsgInfo.setBusinessId(id);
		sysMsgInfo.setUrl("/device/repairInfo/edit-"+id+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(this.getCurrentUser());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);		
	}


	
	
	
}
